//
//  SHGoodsCommentListController.m
//  NoNeedle
//
//  Created by chirui on 17/3/15.
//  Copyright © 2017年 Ming Tian. All rights reserved.
//

#import "SHGoodsCommentListController.h"

@interface SHGoodsCommentListController ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView * _tableView;
    NSMutableArray * _dataArray;
    int _page;
    int _totailPage;
}

@end

@implementation SHGoodsCommentListController

-(void)viewWillDisappear:(BOOL)animated {
    
    NSArray *array = self.navigationController.viewControllers;
    
    UIViewController * view = [array lastObject];
    
    view.hidesBottomBarWhenPushed = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _dataArray = @[].mutableCopy;
    _page = 1;
    self.view.backgroundColor =[UIColor whiteColor];
    self.navigationItem.titleView=[TopLabel withLabelWithLabelText:@"评价"];

    [self creatTableView];
    
//    [self getGoodsCommentList];
    
       self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_return_back" target:self action:@selector(back)];
}

-(void)back{

    [self.navigationController popViewControllerAnimated:YES];
}

-(void)creatTableView {
    
    _tableView  =[[UITableView alloc]initWithFrame:CGRectMake(0,0, WIDTH, HEIGHT)
                                             style:UITableViewStyleGrouped];
    _tableView.dataSource=self;
    _tableView.delegate =self;
    UIView * view =[[UIView alloc]init];
    _tableView.tableFooterView =view;
    [self.view addSubview:_tableView];
    
        MJRefreshNormalHeader * header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
    
            _page =1;
            [self getGoodsCommentList];
    
        }];
        _tableView.mj_header = header;
    
    MJRefreshAutoNormalFooter *  footer =[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _page++;
        [self getGoodsCommentList];
        
    }];
    if (_dataArray.count<10) {
        footer.stateLabel.text=nil;
    }
    
    footer.automaticallyRefresh = YES;
    _tableView.mj_footer =footer;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {

    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
 
    NSDictionary * dic = _dataArray[indexPath.row];
    
    NSString * replyStr = dic[@"reply"];
    if (replyStr.length>0||replyStr!=nil) {
        
        UILabel *contentLabel;
        
        contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(20,60 , WIDTH -40, 20)];
        contentLabel.text = dic[@"content"];
        contentLabel.textColor = [UIColor blackColor];
        contentLabel.numberOfLines = 3;
        contentLabel.font = [UIFont systemFontOfSize:15];
        [contentLabel sizeToFit];
        
        UILabel * replyLabel =[[UILabel alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(contentLabel.frame)+10,WIDTH -40, 20)];
        replyLabel.text = [NSString stringWithFormat:@"【回复】:%@",dic[@"reply"]];
        replyLabel.textColor = [UIColor blackColor];
        replyLabel.numberOfLines = 4;
        replyLabel.font = [UIFont systemFontOfSize:15];
        [replyLabel sizeToFit];
        
        return CGRectGetMaxY(replyLabel.frame)+20;
        
    }else {
        
        UILabel *contentLabel;
        
        contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(20,60 , WIDTH -40, 20)];
        
        contentLabel.text = dic[@"content"];
        contentLabel.textColor = [UIColor blackColor];
        contentLabel.numberOfLines = 3;
        contentLabel.font = [UIFont systemFontOfSize:15];
        [contentLabel sizeToFit];
        
        return CGRectGetMaxY(contentLabel.frame)+20;
    }
    
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        static NSString * cellId =@"cellid";
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell==nil) {
            
            cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        }else{
            while ([cell.contentView.subviews lastObject]) {
                
                [[cell.contentView.subviews lastObject] removeFromSuperview];
            }
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
            
          if (_dataArray.count>0) {

            NSDictionary * dic = _dataArray[indexPath.row];
            UIView *view = [[UIView alloc]initWithFrame:CGRectMake(10, 5, WIDTH - 20, 2)];
            view.clipsToBounds = YES;
            view.layer.cornerRadius = 5.f;
            [cell.contentView addSubview:view];
            
            UIImageView * userImage =[[UIImageView alloc]initWithFrame:CGRectMake(0, 7, 50, 50)];
            userImage.layer.cornerRadius = 25.f;
            userImage.layer.masksToBounds = YES;
            [userImage sd_setImageWithURL:[NSURL URLWithString:dic[@"avatar"]] placeholderImage:[UIImage imageNamed:@"img_head_portrait_pre"]];
            [view addSubview:userImage];
            
            
            UILabel * name =[[UILabel alloc]initWithFrame:CGRectMake(55, 9, 60, 20)];
            name.font =[UIFont systemFontOfSize:15];
            name.textColor =RGBa(0x3c7420);
            name.text =dic[@"user_name"];
            [name sizeToFit];
            [view addSubview:name];
            
            UILabel * date =[[UILabel alloc]initWithFrame:CGRectMake(55, 39,WIDTH-70, 15)];
            date.font =[UIFont systemFontOfSize:13];
            date.textColor = [UIColor lightGrayColor];
            date.text = dic[@"comment_time"];
            [view addSubview:date];
            
              UILabel *contentLabel;
              
              contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(10,CGRectGetMaxY(date.frame)+10 , CGRectGetWidth(view.frame)-20, 20)];
              
              contentLabel.text = dic[@"content"];
              contentLabel.textColor = [UIColor blackColor];
              contentLabel.numberOfLines = 3;
              contentLabel.font = [UIFont systemFontOfSize:15];
              [contentLabel sizeToFit];
              [view addSubview:contentLabel];
              
              NSString * replyStr = dic[@"reply"];
              if (replyStr.length>0||replyStr!=nil) {
                  
                  UILabel * replyLabel =[[UILabel alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(contentLabel.frame)+10, CGRectGetWidth(view.frame)-20, 20)];
                  replyLabel.textColor = [UIColor grayColor];
                  replyLabel.backgroundColor = [UIColor groupTableViewBackgroundColor];
                  replyLabel.attributedText = [SHJudgeTools changeAllStr:[NSString stringWithFormat:@"平台回复: %@",dic[@"reply"]] partStr:@"平台回复:" color:[UIColor orangeColor] font:14];
                  replyLabel.numberOfLines = 4;
                  replyLabel.font = [UIFont systemFontOfSize:15];
                  [replyLabel sizeToFit];
                  [view addSubview:replyLabel];
                  
                  view.frame = CGRectMake(10, 5, self.view.frame.size.width - 20, CGRectGetMaxY(replyLabel.frame) + 20);
              }else {
                  
                  view.frame = CGRectMake(10, 5, self.view.frame.size.width - 20, CGRectGetMaxY(contentLabel.frame) + 20);
              }
              
              
          }
            return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

-(void)viewWillAppear:(BOOL)animated {
    
    self.tabBarController.tabBar.hidden=YES;
}


-(void)getGoodsCommentList {
    
//    if (_totailPage != 0) {
//        if(_page == _totailPage + 1){
//            [_tableView.mj_footer setState: MJRefreshStateNoMoreData];
//
//            return ;
//        }
//    }
//
//    NSString * parStr = [NSString stringWithFormat:@"%@%@",address_url,get_goodsComment_api];
//
//    NSDictionary * dic = @{@"page":[NSString stringWithFormat:@"%d",_page],
//                           @"pro_id":self.product_id,
//                           @"page_num":@"10"
//                           };
//
//    [PostNetWorking postNetWorkingStr:parStr  andparameters:dic Success:^(id responseObject) {
//
//        if ([responseObject[@"code"] intValue]==200) {
//
//           _totailPage = [SHJudgeTools totailpageNumberForTotailNumber:responseObject[@"datas"][@"total_count"]];
//
//            if (_page==1) {
//
//                if (_dataArray) {
//
//                    [_dataArray removeAllObjects];
//                }
//                if ([responseObject[@"datas"][@"list"]  isKindOfClass:[NSArray class]]) {
//
//                    for (NSDictionary * dic in responseObject[@"datas"][@"list"]) {
//
//                        [_dataArray addObject:dic];
//                    }
//                }
//
//            }else {
//
//                if ([responseObject[@"datas"][@"list"]  isKindOfClass:[NSArray class]]) {
//
//                    for (NSDictionary * dic in responseObject[@"datas"][@"list"]) {
//
//                        [_dataArray addObject:dic];
//                    }
//                }
//            }
//            if (_dataArray.count<10) {
//
//                _tableView.mj_footer.hidden =YES;
//            }else {
//                _tableView.mj_footer.hidden =NO;
//            }
//        }else {
//
//            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
//
//        }
//
//        [_tableView.mj_footer endRefreshing];
//        [_tableView.mj_header  endRefreshing];
//        [_tableView reloadData];
//
//
//    } failed:^(NSError *error) {
//        NSLog(@"错误信息%@",error);
//        SHuiMBhudTextNetError;
//    }];
    
    
}


@end
